<meta charset="UTF-8">
<h1 class="bluebar" style="text-align:center;background-color:#008080;color:#fff;">Meal Manage System</h1>
<head>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<?php
require_once 'config.php';
date_default_timezone_set('Asia/Ho_Chi_Minh');
$date = date('Y-m-d');
session_start();
$user_fullname = $_SESSION['user_fullname'];
$user_name = $_SESSION['user_name'];
$user_rights = $_SESSION['user_rights'];
if(isset($_GET['today'])) {  
$today = $_GET['today'];
}
if (!empty($_SESSION['user_name'])) {    
        echo "<div style='text-align:right;'>Welcome ".$user_fullname."      
     <br><a align='center' href='login/index.php?logout'>Logout</a>
     <!--<a href='login/classes/change_password.php'>Change Password</a>-->
	</div>";

        $res_tencotvn = mysql_query("SELECT * FROM tencotvn where tenBang = 'mms'");
        while ($row_tencotvn = mysql_fetch_array($res_tencotvn)) {
           $colNumber = $row_tencotvn['soCot'];
        }

        $tenCot = array();
        $queryTenCot = "SELECT * FROM tencot where tenBang = 'mms'";
        $resultTenCot = mysql_query($queryTenCot);
        while ($rowTenCot = mysql_fetch_array($resultTenCot)) {
            for ($i = 1; $i <= $colNumber; $i++) {
                $temp = "col" . $i;
                $tenCot[$i] = $rowTenCot[$temp];
            }
        }
        echo "<form action='update_mms.php?tableName=mms&colNumber=" . $colNumber . "' method='POST'/>
	Date: " . $date . "
	<input type='checkbox' name='session1' value='Lunch' checked> Lunch
	<input type='checkbox' name='session2' value='Dinner' checked>Dinner
	<input type='submit' name='submit' value='Order !'>
	</form>";
        //echo " lkj<a href='mms_download.php'>Download table as excel </a>";       
        
        echo '<ul>            
	<li><a  class="menu" href="index.php?today">Today</a></li>
	<li><a  class="menu" href="index.php">History</a></li>
	<li><a  class="menu" href="#contact">Guide</a></li>
	<li><a  class="menu" href="#about">About</a></li>
	</ul>';   
        
        echo '<div style="margin-top:30px;width:100%;"><div style="float:left; width:50%;">';
        // Lunch Table
        $lunch_book_today = mysql_query("SELECT COUNT(book) as lunch_book_today FROM mms WHERE book='Yes' and session = 'Lunch' and date = '$date'");
        while ($row_lunch_book_today = mysql_fetch_array($lunch_book_today)) {
            echo "Lunch Order Today: ".$book_today = $row_lunch_book_today['lunch_book_today'];
        }
        echo '</div><div style="float:left;width:50%;">';
        $dinner_book_today = mysql_query("SELECT COUNT(book) as dinner_book_today FROM mms WHERE book='Yes' and session = 'Dinner' and date = '$date'");
        while ($row_book_today = mysql_fetch_array($dinner_book_today)) {
            echo "Dinner Order Today: ".$book_today = $row_book_today['dinner_book_today'];
        }
        
        echo "</div></div>";
        if($user_rights == 'admin'){
            // identify today
            if(isset($_GET['today'])) {
                $result_lunch = mysql_query("SELECT * FROM mms where session = 'Lunch' and date = '$date' ORDER BY date DESC LIMIT 10");
            }
            else {
                $result_lunch = mysql_query("SELECT * FROM mms where session = 'Lunch' ORDER BY date DESC LIMIT 10");
            }                
        }
        else {  
            if(isset($_GET['today'])) {
                $result_lunch = mysql_query("SELECT * FROM mms where user_name = '$user_name' and session = 'Lunch' and date = '$date' ORDER BY date DESC LIMIT 10");
            }
            else {
                $result_lunch = mysql_query("SELECT * FROM mms where user_name = '$user_name' and session = 'Lunch' ORDER BY date DESC LIMIT 10");
            }        
        }
        $result_lunch_tencotvn = mysql_query("SELECT * FROM tencotvn where tenBang = 'mms'");
        while ($row2 = mysql_fetch_array($result_lunch_tencotvn)) {
            $colNumber = $row2['soCot'];
            echo '<div style="width:100%;"><table style="float:left;margin-top:0px;" width="50%" cellpadding="3" cellspacing="0" border="1"><tr style="background:#f4a460; color:#fff;">';
            for ($i = 1; $i <= $colNumber; $i++) {
                $tencotvn = "col" . $i;
                echo "<th>" . $row2[$tencotvn] . "</th>";
            }
        }
        echo "</tr>";
        while ($row = mysql_fetch_array($result_lunch)) {
            echo "<tr>";
            for ($i = 1; $i <= $colNumber; ++$i) {
                //$tencot = "col".$i;
                $temp = $tenCot[$i];
                echo "<td align='center'>" . $row[$temp] . "</td>";
            }
            echo "</tr>";
        }
        echo "</table>";

        // Dinner Table	        
        if($user_rights == 'admin') {
            // identify today
            if(isset($_GET['today'])) {
                $query_member = "SELECT * FROM mms where session='Dinner' and date = '$date' ORDER BY date DESC LIMIT 10";
            }
            else {
                $query_member = "SELECT * FROM mms where session='Dinner' ORDER BY date DESC LIMIT 10";
            }            
         }
         else {
             if(isset($_GET['today'])) {
                $query_member = "SELECT * FROM mms where user_name = '$user_name' and session='Dinner' and date = '$date' ORDER BY date DESC LIMIT 10"; 
            }
            else {
                $query_member = "SELECT * FROM mms where user_name = '$user_name' and session='Dinner' ORDER BY date DESC LIMIT 10"; 
            }
                    
         }
        $result_member = mysql_query($query_member);
        $query2 = "SELECT * FROM tencotvn where tenBang = 'mms'";
        $result2 = mysql_query($query2);
        while ($row2 = mysql_fetch_array($result2)) {
            $colNumber = $row2['soCot'];
            echo '<table style="margin-top:0px;" width="50%" cellpadding="3" cellspacing="0" border="1"><tr style="background:#008080; color:#fff;">';
            for ($i = 1; $i <= $colNumber; $i++) {
                $tencotvn = "col" . $i;
                echo "<th>" . $row2[$tencotvn] . "</th>";
            }
        }
        echo "</tr>";
        while ($row = mysql_fetch_array($result_member)) {
            echo "<tr>";
            for ($i = 1; $i <= $colNumber; ++$i) {
                $temp = $tenCot[$i];
                echo "<td align='center'>" . $row[$temp] . "</td>";
            }
            echo "</tr>";
        }
        echo "</table></div>";
    }
 else {
    echo '<h4 style="text-align:center;">Mày chưa login kìa thằng chó, Click bên dưới để login!</h4>';
    echo '<div style="text-align:center;>
    <!-- because people were asking: "index.php?logout" is just my simplicated form of "index.php?logout=true" -->
    <a href="login/index.php?login">Login</a>
</div>';
}
?>
